<script setup lang="ts">
import {ref} from "vue";

const searchText = ref("");

const onSearch = () => {
  tagList.value = originTagList.map(parentTag =>{
    const tempChildren = [...parentTag.children];
    const tempParentTag = {...parentTag}
    tempParentTag.children = tempChildren.filter(item => item.text.includes(searchText.value));
    return tempParentTag;
  })
  console.log(tagList)
}
const onCancel = () => {
  searchText.value = "";
  tagList.value = originTagList;

}

// 已选择的标签
const activeIds = ref([]);
const activeIndex = ref(0);
console.log(activeIndex.value);
// 标签列表
const originTagList = [
  {
    text: '性别',
    children: [
      { text: '男', id: '男' },
      { text: '女', id: '女' },
    ],
  },
  {
    text: '年级',
    children: [
      { text: '大一', id: '大一' },
      { text: '大二', id: '大二' },
      { text: '大三', id: '大三' },
      { text: '大四', id: '大四' },
    ],
  },
  {
    text: '爱好',
    children: [
      { text: '编程', id: '编程' },
      { text: '游戏', id: '游戏' },
      { text: '打球', id: '打球' },
    ],
  },
];

let tagList = ref(originTagList)

// 移除标签
const doClose = (tag:String) => {
  activeIds.value = activeIds.value.filter(item => {
    return item !== tag;
  })
};
import {useRouter} from "vue-router";
const router = useRouter()
// 搜索
const doSearchResult = () => {
  router.push({
    path:"/user/list",
    query: {
      tags: activeIds.value,
    }
  });
}
</script>

<template>
  <form action="/">
    <van-search
      v-model="searchText"
      show-action
      placeholder="请输入要搜索的标签"
      @search="onSearch"
      @cancel="onCancel"
    />
  </form>
  <van-divider content-position="left">已选标签</van-divider>
  <div v-if="activeIds.length === 0">请选择标签</div>
  <van-space wrap>
    <van-tag v-for="tag in activeIds" closeable size="medium" type="primary" @close="doClose(tag)">
      {{ tag }}
    </van-tag>
  </van-space>

  <van-divider content-position="left">选择标签</van-divider>

  <van-tree-select
      v-model:active-id="activeIds"
      v-model:main-active-index="activeIndex"
      :items="tagList"
  />
  <div style="text-align: center;">
    <van-button
        style="text-align: center;
        margin-top:10px;
        width: 50%;"
        type="primary" @click="doSearchResult">搜索
    </van-button>
  </div>

</template>

<style scoped>

</style>